<?php


class LoginForm extends CFormModel
{

	public $username;
	public $password;
	public $rememberMe=false;

	private $_identity;

	public function rules()
	{
		return array(
				array("username,password","required","message"=>'{attribute}不能为空'),
				array("password","authenticate")
			);
	}

	public function authenticate($attribute,$params)
	{
		$this->_identity = new UserIdentity($this->username,$this->password);
		if(!$this->_identity->authenticate())
		{
			$this->addError("password","用户名或者密码不正确");
		}
	}

	public function attributeLabels()
	{
		return array('username'=>'用户名','password'=>'密码');
	}


	public function login()
	{
		if($this->_identity==null)
		{
			$this->_identity = new UserIdentity($this->username,$this->password);
			$this->_identity->authenticate();
		}
		if($this->_identity->errorCode==UserIdentity::ERROR_NONE)
		{
			$duration=0; // 单位秒
			Yii::app()->user->login($this->_identity,$duration);
			return true;
		}
		else
		{
			return false;
		}
	}
}